package activitis;

import utils.BaseAsync;
import utils.PreferencesHelper;

import com.example.rapichofer.R;

import controladores.ControladorChoferes;

import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;

public class BaseActivityCerrarSesion extends Activity
{
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int id = item.getItemId();
		if (id == R.id.cerrarSesion)
		{
			new CerrarSesion().execute();
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class CerrarSesion extends BaseAsync
	{
		private boolean res;
		
		public CerrarSesion ()
		{
			super (BaseActivityCerrarSesion.this, "Cerrando sesion...");
		}
		
		@Override
		protected Void doInBackground(Void... arg0)
		{
			res = new ControladorChoferes().cerrarSesion(new PreferencesHelper(getApplicationContext()).leerID());
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			
			if (res)
			{
				PreferencesHelper helper = new PreferencesHelper(getApplicationContext());
				helper.editKey(PreferencesHelper.ID, 0);
				
				Intent intent = new Intent (BaseActivityCerrarSesion.this, LoginChoferActivity.class);
				startActivity(intent);
				finish();
			}
		}
	}
}
